﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace FirmaTransportowa
{

    public partial class AdminPanelUserView : Form
    {

        //SqlConnection dbConnection2;

        public AdminPanelUserView()
        {
            InitializeComponent();
        }

        private void nowaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AdminAddNewUser adminAddNewUser = new AdminAddNewUser();
            adminAddNewUser.Show();
        }

        private void dodajToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AdminAddNewWorker adminAddNewWorker = new AdminAddNewWorker();
            adminAddNewWorker.Show();
        }

        private void adminPanelCarViewMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
            AdminPanelCarView adminPanelCarView = new AdminPanelCarView();
            adminPanelCarView.Show();
        }

        private void adminPanelWorkerViewMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
            AdminPanelWorkerView adminPanelWorkerView = new AdminPanelWorkerView();
            adminPanelWorkerView.Show();
        }

        private void displayUsers()
        {
            usersDataGridView.DataSource = User.getUsersForAdmin();
            usersDataGridView.Columns[0].HeaderText = "ID użytkownika";
            usersDataGridView.Columns[1].HeaderText = "login";
            usersDataGridView.Columns[2].HeaderText = "ID pracownika";
        }

        private void AdminPanel_Load(object sender, EventArgs e)
        {
            displayUsers();
        }

        private void addUserButton_Click(object sender, EventArgs e)
        {
            AdminAddNewUser adminAddNewUser = new AdminAddNewUser();
            adminAddNewUser.Show();
            usersDataGridView.Update();
            usersDataGridView.Refresh();
        }

        private void deleteUserButton_Click(object sender, EventArgs e)
        {
            User.getById(int.Parse(usersDataGridView.Rows[usersDataGridView.CurrentRow.Index].Cells[0].Value.ToString())).delete();
            MessageBox.Show("Skasowano użytkownika");
            displayUsers();
        }

        private void refreshButton_Click(object sender, EventArgs e)
        {
            displayUsers();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            AdminAddNewUser adminAddNewUser = new AdminAddNewUser(User.getById(int.Parse(usersDataGridView.Rows[usersDataGridView.CurrentRow.Index].Cells[0].Value.ToString())), true);
            adminAddNewUser.Show();
        }

        private void usersDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void AdminPanelUserView_HelpButtonClicked(object sender, CancelEventArgs e)
        {
            HelpUtils.showHelp(this, 13);
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.F1)
            {
                HelpUtils.showHelp(this, 13);
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

    }
}
